#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	int V = 0;
	int n = 0;
	scanf("%d", &V);
	scanf("%d", &n);
	int* dp = calloc(n+1, sizeof(int));
	int* room = calloc(n, sizeof(int));
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &room[i]);
		dp[i] = V;
	}
	dp[n] = V;
	int min = V;
	for (int i = n - 1; i >= 0; --i)
	{
		for (int j = i + 1; j <= n; ++j)
		{
			if (dp[j] - room[i] >= 0)
				dp[i] = (int)fmin(dp[i], dp[j] - room[i]);
		}
		if (dp[i] == 0)
		{
			printf("0");
			return 0;
		}
		else if (dp[i] < min)
			min = dp[i];
	}
	printf("%d", min);
	return 0;
}